﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Forms;
using WinRpaLib;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinRPA
{
    public partial class DemoForm : Form
    {
        RpaCommand[] this_flowlist = null;
        public DemoForm()
        {
            InitializeComponent();
        }

        private void P_临时测试_BT_Click(object sender, EventArgs e)
        {
            string selecter = P_Selecter验证器_RTB.Text;
            AutomationElement element = UiElementFinder.FindElementBySelecter(selecter);
            if (element != null)
            {
                MessageBox.Show("已经找到");
            }
            else
            {
                MessageBox.Show("没有找到！！");
            }
        }


        static void TraverseAndPrintElement(AutomationElement element, int indentLevel)
        {
            // 打印当前元素的名称和控件类型
            for (int i = 0; i < indentLevel; i++)
            {
                Console.Write("{0}  ", indentLevel); // 打印缩进以表示层级
            }

            Console.WriteLine($"Name: {element.Current.Name}, ControlType: {element.Current.ControlType}");

            // 获取子元素
            TreeWalker treeWalker = TreeWalker.ControlViewWalker;

            // 获取第一个子元素
            AutomationElement firstChild = treeWalker.GetFirstChild(element);
            if (firstChild != null)
            {
                // 递归遍历第一个子元素及其所有兄弟元素
                AutomationElement currentChild = firstChild;
                while (currentChild != null)
                {
                    TraverseAndPrintElement(currentChild, indentLevel + 1); // 增加缩进级别以表示子层级
                    currentChild = treeWalker.GetNextSibling(currentChild);
                }
            }
        }


        private void P_Open_计算器_BT_Click(object sender, EventArgs e)
        {
            this_flowlist = Example.flowlist001;
            listView1.Items.Clear();
            foreach (var item in this_flowlist)
            {
                ListViewItem ll = listView1.Items.Add(item.Title); 
                ll.Checked = true;
                ll.Tag = item;
            }
        }

        private void P_Flow_Run_BT_Click(object sender, EventArgs e)
        {
            RpaHelper helper = new RpaHelper();

            foreach (ListViewItem item in listView1.CheckedItems)
            {
                helper.DoAction((RpaCommand)item.Tag);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 检查是否有项被选中
            if (listView1.SelectedItems.Count > 0)
            {
                richTextBox1.Clear();
                // 获取选中的项
                ListViewItem selectedItem = listView1.SelectedItems[0];
                RpaCommand cmd = (RpaCommand)selectedItem.Tag;
                richTextBox1.AppendText(cmd.ToString());
                richTextBox1.Tag = selectedItem;
            }
        }

        private void P_cmd_update_BT_Click(object sender, EventArgs e)
        {
            ListViewItem item = (ListViewItem)richTextBox1.Tag;
            ((RpaCommand)item.Tag).Parser(richTextBox1.Text);
            item.Text = ((RpaCommand)item.Tag).Title;
        }

        private void P_远程连接_BT_Click(object sender, EventArgs e)
        {
            this_flowlist = Example.flowlist002;
            listView1.Items.Clear();
            foreach (var item in this_flowlist)
            {
                ListViewItem ll = listView1.Items.Add(item.Title);
                ll.Checked = true;
                ll.Tag = item;
            }
        }
    }
}
